/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package model.DAO;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import model.entities.Role;


/**
 *
 * @author Hieu.Nguyen
 */
public class RoleDAO {
    private static RoleDAO roleDAO;

    public static RoleDAO getInstance() {
        if (roleDAO == null) {
            roleDAO = new RoleDAO();
        }
        return roleDAO;
    }
    
     public List<Role> getRole(boolean isActived) {
        List<Role> listRole = new ArrayList<Role>();
        try {
            Connection conn = ConnectionFactory.getConnection();
            String sql = "select * from tblRole";
            PreparedStatement pstmt = conn.prepareStatement(sql);
            
            ResultSet rs = pstmt.executeQuery();
            while (rs.next()) {
                Role r = new Role();
                r.setRoleID(rs.getInt("RoleID"));
                r.setRoleName(rs.getString("RoleName"));
                r.setRoleDescription(rs.getString("RoleDescription"));
                listRole.add(r);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return listRole;
    }
    public Role getRoleByID(int roleID) {

        Role rl = new Role();
        try {
            Connection conn = ConnectionFactory.getConnection();
            String sql = "select * from tblRole where RoleID = ?";
            PreparedStatement pstmt = conn.prepareStatement(sql);
            pstmt.setInt(1, roleID);
            
            ResultSet rs = pstmt.executeQuery();
            while (rs.next()) {
                
                rl.setRoleID(rs.getInt("RoleID"));
                rl.setRoleName(rs.getString("RoleName"));
                rl.setRoleDescription(rs.getString("RoleDescription"));
             
                
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return rl;
     }
    
}
